package cn.bdqn.service.finance;

import java.beans.IntrospectionException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import cn.bdqn.dao.finance.FinanceMapper;
import cn.bdqn.pojo.ExcelBean;
import cn.bdqn.pojo.Finance;
import cn.bdqn.tools.ExcelUtil;
@Service
public class SalaryServiceImpl implements SalaryService {
	@Resource
	private FinanceMapper mapper;
	public void importExcelInfo(InputStream in, MultipartFile file, String salaryDate,Integer adminId) throws Exception{
    List<List<Object>> listob = ExcelUtil.getBankListByExcel(in,file.getOriginalFilename());
    List<Finance> salaryList = new ArrayList<Finance>();
    //遍历listob数据，把数据放到List中
    for (int i = 0; i < listob.size(); i++) {
        List<Object> ob = listob.get(i);
        Finance f = new Finance();
        //设置编号
//        salarymanage.setSerial(SerialUtil.salarySerial());
        //通过遍历实现把每一列封装成一个model中，再把所有的model用List集合装载
        f.setYear(String.valueOf(ob.get(0)));
        f.setMonth(String.valueOf(ob.get(1)));
        f.setEmpId(Integer.parseInt(ob.get(2).toString()));
        f.setEmpName(String.valueOf(ob.get(3)));
        f.setJobId(Integer.parseInt(ob.get(4).toString()));
        f.setDepId(Integer.parseInt(ob.get(5).toString()));
        f.setKaoId(Integer.parseInt(ob.get(6).toString()));
//        salarymanage.setCompany(String.valueOf(ob.get(1)));
//        salarymanage.setNumber(String.valueOf(ob.get(2)));
//        salarymanage.setName(String.valueOf(ob.get(3)));
//        salarymanage.setSex(String.valueOf(ob.get(4)));
//        salarymanage.setCardName(String.valueOf(ob.get(5)));
//        salarymanage.setBankCard(String.valueOf(ob.get(6)));
//        salarymanage.setBank(String.valueOf(ob.get(7)));
//        //object类型转Double类型
//        salarymanage.setMoney(Double.parseDouble(ob.get(8).toString()));
//        salarymanage.setRemark(String.valueOf(ob.get(9)));
//        salarymanage.setSalaryDate(salaryDate);
        salaryList.add(f);
    }
    //批量插入
    mapper.insertFinance(salaryList);
}
	@Override
	public XSSFWorkbook exportExcelInfo() throws IllegalArgumentException, IllegalAccessException, InvocationTargetException, ClassNotFoundException, IntrospectionException, ParseException {
		//根据条件查询数据，把数据装载到一个list中  
	    List<Finance> list = mapper.getlist(null, 0, null, null, -1, 0);  
	    /*for(int i=0;i<list.size();i++){  
	        //查询财务名字  
	        int adminId = list.get(i).getAdminId();  
	        String adminName = salarymanageDao.selectAdminNameById(adminId);  
	        list.get(i).setAdminName(adminName);  
	        list.get(i).setId(i+1);  
	    }  */
	    List<ExcelBean> excel=new ArrayList<ExcelBean>();  
	    Map<Integer,List<ExcelBean>> map=new LinkedHashMap();  
	    XSSFWorkbook xssfWorkbook=null;  
	    //设置标题栏  
	    excel.add(new ExcelBean("年份","year",0));  
	    excel.add(new ExcelBean("月份","month",0));  
	    excel.add(new ExcelBean("员工编号","empId",0)); 
	    excel.add(new ExcelBean("员工姓名","empName",0));  
	    excel.add(new ExcelBean("岗位名称","jobName",0));  
	    excel.add(new ExcelBean("部门名称","depName",0));  
	    excel.add(new ExcelBean("考勤工资","kaoqinPay",0));  
	    excel.add(new ExcelBean("实际工资","realPay",0));  
	    
	    map.put(0, excel);  
	    String sheetName ="薪资收入";  
	    //调用ExcelUtil的方法  
	    xssfWorkbook = ExcelUtil.createExcelFile(Finance.class, list, map, sheetName);  
	    return xssfWorkbook;
	}

}
